using Microsoft.Extensions.Logging;
using SuperSocket.Command;

namespace SuperSocketProxy.Commands;

[Command(Key = (int)Command.Handshake)]
public sealed class Handshake : SocketProxyAsyncCommand<Socket5HandshakePacket>
{
    protected override async ValueTask ExecuteAsync(SocketProxySession session, Socket5HandshakePacket package)
    {
        session.ProxyType = package.ProxyType;
        
        session.Inilizetion();
        
        if (session.IsVerify())
        {
            session.LogInformation($"本地地址：{session.LocalEndPoint} 远程地址：{session.RemoteEndPoint} 需要账号密码");
            
            await session.PacketResponse.SendHandshakeAsync(verifyUser: true);
        }
        else
        {
            session.LogInformation($"本地地址：{session.LocalEndPoint} 远程地址：{session.RemoteEndPoint} 无需账号密码");
            
            await session.PacketResponse.SendHandshakeAsync(verifyUser: false);

            throw new CloseSessionException();
        }
    }
}